#include "adminmain.h"
#include "ui_adminmain.h"
#include "DateRange.h"
#include "admincourseform.h"
//#include <qdebug.h>

//adminmain calendar constructor
AdminMain::AdminMain(QWidget *parent, AdminData *newData) :
    QWidget(parent),
    ui(new Ui::AdminMain)
{
    ui->setupUi(this);
    (this->data) = newData;

    //intialization to prevent later errors

    dateSet=false;
    //useful stuff
    this->semDurat=&(data->getSemDuration());


    this->holidays=(data->getDaysOff());
    //show previously created holidays

    int i=0;
    while(i<holidays.size())
    {
        ui->textBrowser_2->append(holidays[i].getName()+": "+holidays[i].getStartDate().toString()
                                  +" "+holidays[i].getEndDate().toString());
        i++;
    }
    ui->textBrowser->append(semDurat->getName()+": "+semDurat->getStartDate().toString()+" "+semDurat->getEndDate().toString());
    //will be persistent


}

//admin main destructor
AdminMain::~AdminMain()
{
    delete ui;
    //will be persistent
    //    delete ad;
}


//set semester duration method
void AdminMain::on_pushButton_7_clicked()
{

    //get the dates
    QDate stDate=ui->dateEdit_38->date();
    QString dat1= stDate.toString();

    QDate enDate=ui->dateEdit_39->date();

    QString dat2= enDate.toString();


    //the semester must end after it starts, if wrong, give error message
    if(stDate.operator >=(enDate))
    {


        AdminErrorDialog *aed21=new AdminErrorDialog(this," Semester must end after it starts.");
        aed21->setModal(true);
        aed21->exec();
    }
    else if(dat1.isEmpty() || dat1.isNull() || dat2.isEmpty() || dat2.isNull())
    {//if either date isn't there (somehow), give error message

        AdminErrorDialog *aed22=new AdminErrorDialog(this,"Must enter both dates.");
        aed22->setModal(true);
        aed22->exec();
    }else
    {
        //if all is well, declare the semester

        QString sem="Semester: "+dat1+" "+dat2;

        ui->textBrowser->clear();
        ui->textBrowser->append(sem);
        semDurat=(new DateRange("Semester", stDate, enDate));

        //set our bool
        dateSet=true;
    }
}


//persistence button
void AdminMain::on_pushButton_clicked()
{
    //do persistence stuff


    if(dateSet==true)
    {
    data->setSemDuration(semDurat);
    }

    data->setDaysOff(&holidays);

    AdminErrorDialog *aedPer=new AdminErrorDialog(this,"Data has been saved.");
    aedPer->setModal(true);
    aedPer->exec();
}

//button to go to course screen
void AdminMain::on_pushButton_2_clicked()
{

    AdminCourseForm *aCF=new AdminCourseForm(this, data);
    aCF->show();
    aCF->topLevelWidget();

}

//method to go to outline screen
void AdminMain::on_pushButton_3_clicked()
{
    AdminOutSectForm *aOSF=new AdminOutSectForm(this, data);
    aOSF->show();
    aOSF->topLevelWidget();

}


//method for adding a holiday
void AdminMain::on_pushButton_5_clicked()
{

    //get dates and holiday name
    QDate stDate=ui->dateEdit_35->date();
    QString dat1= stDate.toString();

    QDate enDate=ui->dateEdit_33->date();

    QString dat2= enDate.toString();

    QString natHol= ui->lineEdit_4->text();

    //check that the holiday does not end after it starts, if is case, give error message
    if(stDate.operator >(enDate))
    {

        AdminErrorDialog *aed23=new AdminErrorDialog(this," Holiday cannot start after it ends.");
        aed23->setModal(true);
        aed23->exec();
    }
    else if(natHol.isEmpty() || natHol.isNull() )
    {//if no holiday name entered, give error message

        AdminErrorDialog *aed24=new AdminErrorDialog(this," Must enter a holiday name.");
        aed24->setModal(true);
        aed24->exec();

    }else
    {

        //check if holiday is already there
        int i=0;
        bool isThere=false;
        while(i<holidays.size()&& isThere==false)
        {
            if(holidays[i].getName().compare(natHol)==0)
            {
                isThere=true;
            }

            i++;
        }

        //if it is there, give an error message
        if(isThere==true)
        {

            AdminErrorDialog *aed25=new AdminErrorDialog(this,"Sorry "+natHol+" is already there.");
            aed25->setModal(true);
            aed25->exec();
        }
        else
        {

            //otherwise, add the holiday
            QString nH=natHol+": "+dat1+" "+dat2;
            ui->textBrowser_2->append(nH);

            DateRange *naHoli=new DateRange(natHol, stDate, enDate);

            holidays.prepend(*(naHoli));

        }
    }
}


//method for setting Reading Week
void AdminMain::on_pushButton_8_clicked()
{

    //get the dates
    QDate stDate=ui->dateEdit_37->date();
    QString dat1= stDate.toString();

    QDate enDate=ui->dateEdit_40->date();

    QString dat2= enDate.toString();

    //if reading week doesnt end after it starts, give an error message
    if(stDate.operator >=(enDate))
    {

        AdminErrorDialog *aed26=new AdminErrorDialog(this," Reading Week must end after it starts.");
        aed26->setModal(true);
        aed26->exec();
    }
    else
    {
        //check if reading week has been added already
        int i=0;
        bool isThere=false;
        while(i<holidays.size()&& isThere==false)
        {
            if(holidays[i].getName().compare("Reading Week")==0)
            {
                isThere=true;
            }

            i++;
        }

        //if it is there already, give an error message
        if(isThere==true)
        {

            AdminErrorDialog *aed27=new AdminErrorDialog(this,"Sorry Reading Week is already defined.  You must remove it to change it.");
            aed27->setModal(true);
            aed27->exec();
        }
        else
        {

            //otherwise, add it
            QString rw="Reading Week: "+dat1+" "+dat2;
            ui->textBrowser_2->append(rw);

            DateRange *reaWk=new DateRange("Reading Week", stDate, enDate);


            holidays.prepend(*(reaWk));
        }
    }
}


//method to add general event
void AdminMain::on_pushButton_6_clicked()
{

    //get dates and title
    QDate stDate=ui->dateEdit_36->date();
    QString dat1= stDate.toString();

    QDate enDate=ui->dateEdit_34->date();

    QString dat2= enDate.toString();

    QString other= ui->lineEdit_3->text();


    //give error message if starts after it ends
    if(stDate.operator >(enDate))
    {
        AdminErrorDialog *aed28=new AdminErrorDialog(this,"Event cannot start after it ends.");
        aed28->setModal(true);
        aed28->exec();
    }
    else if(other.isEmpty() || other.isNull() )
    {//give error message is no name

        AdminErrorDialog *aed29=new AdminErrorDialog(this,"Must enter an event name.");
        aed29->setModal(true);
        aed29->exec();
    }
    else
    {

        //check that the holiday isn't there already
        int i=0;
        bool isThere=false;
        while(i<holidays.size()&& isThere==false)
        {
            if(holidays[i].getName().compare(other)==0)
            {
                isThere=true;
            }

            i++;
        }

        //if it is there already, give an error message
        if(isThere==true)
        {

            AdminErrorDialog *aed30=new AdminErrorDialog(this,"Sorry "+other+" is already there.");
            aed30->setModal(true);
            aed30->exec();
        }
        else
        {//otherwise, add it

            QString oth=other+": "+dat1+" "+dat2;
            ui->textBrowser_2->append(oth);
            DateRange *othr=new DateRange(other, stDate, enDate);

            holidays.prepend(*(othr));
        }

    }
}


//remove button to remove last holiday or whatever
void AdminMain::on_pushButton_15_clicked()
{

    //if no holidays to remove, give an error message
    if(holidays.isEmpty())
    {
        ui->textBrowser_2->clear();

        AdminErrorDialog *aed31=new AdminErrorDialog(this,"Nothing to remove.");
        aed31->setModal(true);
        aed31->exec();
    }
    else
    {
        //otherwise, remove the holiday and redisplay the rest
        holidays.remove(0);
        ui->textBrowser_2->clear();
        int i=0;
        while(i<holidays.size())
        {
            ui->textBrowser_2->append(holidays[i].getName()+": "+holidays[i].getStartDate().toString()
                                      +" "+holidays[i].getEndDate().toString());
            i++;
        }
    }

}

void AdminMain::on_pushButton_4_clicked()
{
    PrdfForm *PDF=new PrdfForm(this, data);
    PDF->show();
    PDF->topLevelWidget();
}
